package com.itboye.shop.category.adapter;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.itboye.shop.R;
import com.itboye.shop.bean.GoodsModel;
import com.itboye.shop.util.helper.ImageLoader;
import com.itboye.shop.util.loadutil.Const;

import java.util.List;


/**
 * 商品列表
 */
public class GoodsListAdapter extends BaseAdapter {
    List<GoodsModel.GoodsList> goodsList;
    Activity activity;

    public GoodsListAdapter(List<GoodsModel.GoodsList> goodsList, Activity activity) {
        this.goodsList = goodsList;
        this.activity = activity;
    }

    @Override
    public int getCount() {
        return goodsList.size();
    }

    @Override
    public Object getItem(int position) {
        return goodsList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (holder == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(activity).inflate(R.layout.item_goodslist, null);
            holder.goods_img = (SimpleDraweeView) convertView.findViewById(R.id.goods_img);
            holder.good_title = (TextView) convertView.findViewById(R.id.good_title);
            holder.good_jieshao = (TextView) convertView.findViewById(R.id.good_jieshao);
            holder.good_price = (TextView) convertView.findViewById(R.id.good_price);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        ImageLoader.loadImage(holder.goods_img, Const.IMAGE_HEAD + goodsList.get(position).getMain_img());
        holder.good_title.setText(goodsList.get(position).getName());
        holder.good_jieshao.setText(goodsList.get(position).getSecondary_headlines().equals("")?"无简介":goodsList.get(position).getSecondary_headlines());
        if (goodsList.get(position).getPrice().equals("")) {

        } else {
            holder.good_price.setText("￥" + Double.parseDouble(goodsList.get(position).getPrice()) / 100);
        }
        return convertView;
    }

    public static class ViewHolder {
        SimpleDraweeView goods_img;
        TextView good_title, good_jieshao, good_price;
    }
}
